package com.comac.print.component.app.controller;

import cc.comac.techfoundation.audit.annotation.OptLog;
import cc.comac.techfoundation.audit.enums.OptType;
import com.alibaba.cola.dto.Response;
import com.alibaba.cola.dto.SingleResponse;
import com.comac.print.component.app.domain.PrintMRecord;
import com.comac.print.component.app.domain.dto.PrintRecordSearchDTO;
import com.comac.print.component.app.page.PageResponse;
import com.comac.print.component.app.service.PrintMRecordService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 服务调用记录表 前端控制器
 * </p>
 *
 * @author zhoubin
 * @since 2025-04-16 03:51:14
 */
@Api(tags = "PrintRecordController管理功能控制器")
@RestController
@RequestMapping("/manage/print/record")
public class PrintRecordController {


    private final PrintMRecordService printRecordService;

    public PrintRecordController(PrintMRecordService printRecordService) {
        this.printRecordService = printRecordService;
    }

    /**
     * 分页查询服务调用记录表
     *
     * @param searchDTO 查询条件
     * @return 分页结果
     */
    @PostMapping("/page")
    @ApiOperation("分页查询服务调用记录表")
    PageResponse<PrintMRecord> page(@RequestBody PrintRecordSearchDTO searchDTO){
        return printRecordService.searchPage(searchDTO);
    }

    /**
     * 添加单个服务调用记录表
     *
     * @param entity 服务调用记录表实体
     * @return 添加结果
     */
    @PostMapping("")
    @ApiOperation("添加单个服务调用记录")
    @OptLog(optType = OptType.INSERT, detail = "添加单个模板信息,模板名称：{#entity.systemId}")
    Response add(@RequestBody PrintMRecord entity){
        Date date = Date.from(LocalDateTime.now().atZone(ZoneId.systemDefault()).toInstant());
        entity.setOperatorTime(date);
        boolean result = printRecordService.save(entity);
        return result ? Response.buildSuccess() : Response.buildFailure("-1", "添加单个服务调用记录失败");
    }

    /**
     * 删除单个服务调用记录表
     *
     * @param id 服务调用记录表ID
     * @return 删除结果
     */
    @DeleteMapping("/{id}")
    @ApiOperation("删除单个服务调用记录")
    @OptLog(optType = OptType.DELETE, detail = "删除单个服务调用记录,id：{#id}")
    Response delete(@PathVariable Long id){
        boolean result = printRecordService.removeById(id);
        return result ? Response.buildSuccess() : Response.buildFailure("-1", "删除单个服务调用记录失败");
    }
    /**
     * 批量删除服务调用记录表
     *
     * @param ids 服务调用记录表ID列表
     * @return 批量删除结果
     */
    @DeleteMapping("/delete")
    @ApiOperation("批量删除服务调用记录")
    Response deleteBatch(@RequestBody List<Long> ids){
        boolean result = printRecordService.removeByIds(ids);
        return result ? Response.buildSuccess() : Response.buildFailure("-1", "批量删除服务调用记录失败");
    }

    /**
     * 根据ID查询服务调用记录表详情
     *
     * @param id 服务调用记录表ID
     * @return 服务调用记录表实体
     */
    @GetMapping("/{id}")
    @ApiOperation("根据ID查询服务调用记录详情")
    SingleResponse<PrintMRecord> findById(@PathVariable Long id){
        return SingleResponse.of(printRecordService.getById(id));
    }

}